package com.example.freshfoodproject.controller;

import com.example.freshfoodproject.entry.Goods;
import com.example.freshfoodproject.entry.Shopping;
import com.example.freshfoodproject.service.ShoppingService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class ShoppingController {
    @Autowired
    private ShoppingService shoppingService;

    /**
     * 如果商品表中本来没有该生鲜，则在购物表中添加一条新的数据，如果本来就有，则改变该数据的数量、价格等信息
     * @param goodsName 生鲜的名称
     * @param price 生鲜的价格
     * @param picture  图片的地址
     * @param count 购买此生鲜的个数
     */
    @PostMapping ("/addOne")
    @ResponseBody
    public void addOne(@RequestParam String goodsName,
                       @RequestParam Double price,
                       @RequestParam String picture,
                       @RequestParam(required = false, defaultValue = "1") String count){
        boolean f=true;
        Shopping shopping = new Shopping();
        shopping.setPurchaseItems(goodsName);
        shopping.setPurchaseNumber(Integer.parseInt(count));
        shopping.setPurchasePrice(price*Integer.parseInt(count));
        shopping.setFee(10.0);
        shopping.setPicture(picture);
        List<Shopping> list = shoppingService.findAll();
        if (list.size()==0){
            shoppingService.addOne(shopping);
            return;
        }
        for (Shopping s : list) {
            if (goodsName.equals(s.getPurchaseItems())){
                s.setPurchaseNumber(s.getPurchaseNumber()+shopping.getPurchaseNumber());
                s.setPurchasePrice(s.getPurchasePrice()+(price*shopping.getPurchaseNumber()));
                shoppingService.updateOne(s);
                f=false;
            }
        }
        if (f){
            shoppingService.addOne(shopping);
        }
    }

    /**
     *
     * @return 返回购物表中的所有数据
     */
    @RequestMapping("/showPlace")
    @ResponseBody
    public List<Shopping> showPlace(){
        return shoppingService.findAll();
    }

    /**
     * 通过生鲜的名称对生鲜的价格和数量进行修改
     * @param purchasePrice 生鲜的价格
     * @param purchaseItems 生鲜的名称
     * @param purchaseNumber 生鲜的数量
     */
    @RequestMapping("/updatePlace")
    @ResponseBody
    public void updatePlace(Double purchasePrice,String purchaseItems,int purchaseNumber){
        Shopping shopping = new Shopping();
        shopping.setPurchasePrice(purchasePrice);
        shopping.setPurchaseItems(purchaseItems);
        shopping.setPurchaseNumber(purchaseNumber);
        shoppingService.updateOne(shopping);
    }

    /**
     * 通过生鲜的名称删除该生鲜的所有数据
     * @param purchaseItems 生鲜的名称
     */
    @RequestMapping("/deleteOne")
    @ResponseBody
    public void deleteOne(String purchaseItems){
        shoppingService.removeOne(purchaseItems);
    }

    /**
     * 将购物车中被选择的数据从购物表中删除
     * @param item 购物车中被选中的数据集合
     */
    @RequestMapping("/deleteShopping")
    @ResponseBody
    public void deleteShopping(@RequestBody List<Shopping> item){
        for (Shopping shopping : item) {
            shoppingService.removeOne(shopping.getPurchaseItems());
        }
    }

    /**
     * 将goods类型的数据变为shopping类型的数据
     * @param goodsName 商品名称
     * @param price 商品价格
     * @param picture 图片地址
     * @param count 商品数量
     * @return shopping类型的数据集合
     */
    @RequestMapping("/goodsToShoppig")
    @ResponseBody
    public List<Shopping> goodsToShoppig(@RequestParam String goodsName,
                                   @RequestParam Double price,
                                   @RequestParam String picture,
                                   @RequestParam(required = false, defaultValue = "1") String count){
        Shopping shopping = new Shopping();
        shopping.setPurchaseNumber(Integer.parseInt(count));
        shopping.setPurchaseItems(goodsName);
        shopping.setFee(10.0);
        shopping.setPicture(picture);
        shopping.setPurchasePrice(price*Integer.parseInt(count));
        List<Shopping> list = new ArrayList<>();
        list.add(shopping);
        return list;
    }
}
